<template>
  <div class="container with-nav-padding">
    <div
      v-if="Object.keys(vipInfo).length > 0 && !$route.query.apply"
      class="user-info"
    >
      <div class="user">
        <div class="avatar">
          <img :src="vipInfo.avatar" alt="" />
        </div>
        <div class="info">
          <div class="username">
            <span class="name">{{ vipInfo.nickname }}</span>
            <span class="vip-status">{{ vipInfo.level.name }}</span>
            <span
              v-if="vipInfo.agent"
              class="vip-status"
              style="margin-left:0.2rem;"
              >{{ vipInfo.agent === 1 ? "社区代理" : "区县代理" }}</span
            >
          </div>
          <div class="other-info">
            <!-- <span v-if="vipInfo.level.stop_at" class="expire"
              >有效期至{{ vipInfo.level.stop_at.split(" ")[0] }}</span
            > -->
            <span class="invite">邀请码：{{ vipInfo.invite_code }}</span>
            <span
              class="copy copyInviteCode"
              data-clipboard-action="copy"
              :data-clipboard-text="vipInfo.invite_code"
              @click="copyLink"
              >复制</span
            >
          </div>
        </div>
      </div>
    </div>
    <div class="equity-img">
      <img :src="vipInfo.image" alt="" />
    </div>
    <div class="vip-item-container">
      <div class="title">会员申请</div>
      <div class="vip-item">
        <img
          v-if="goodList.length"
          @click="$router.push('/item-detail/' + goodList[0].id)"
          :src="goodList[0].img_show"
          alt=""
        />
      </div>
    </div>
  </div>
</template>

<script>
import clipboardJs from "clipboard";
import itemBigImg from "@/components/item-big-img.vue";
import itemSmallImg from "@/components/item-small-img.vue";
import myEquity from "./component/my-equity.vue";
import { getVipDetail, getVipGood } from "@/api/vip";
export default {
  data() {
    return {
      vipInfo: {},
      goodList: []
    };
  },
  methods: {
    copyLink() {
      let _this = this;
      let clipboard = new clipboardJs(".copyInviteCode");
      clipboard.on("success", function() {
        _this.$dialog.toast({
          mes: "复制成功",
          timeout: 1000,
          icon: "success"
        });
      });
      clipboard.on("error", function() {
        _this.$dialog.toast({
          mes: "复制失败",
          timeout: 1000,
          icon: "error"
        });
      });
    },
    getVipGood(page) {
      getVipGood({
        page,
        level: 2
      }).then(res => {
        if (res.status === 200) {
          this.goodList.push(...res.data.data.commodity);
          if (res.data.data.commodity.length === 0) {
            this.isAll = true;
          }
        }
        this.loading = false;
      });
    }
  },
  created() {
    this.getVipGood(1);
    getVipDetail({ type: 2 }).then(res => {
      if (res.status === 200) {
        this.vipInfo = res.data.data;
      }
    });
  },
  components: {
    itemBigImg,
    itemSmallImg,
    myEquity
  }
};
</script>

<style lang="less" scoped>
.container {
  .user-info {
    color: #444444;
    height: 3.8rem;
    background-color: #ffffff;
    padding: 0.9rem 3.2% 0.6rem;
    overflow: hidden;
    position: relative;
    margin-bottom: 0.48rem;

    .user {
      display: flex;

      .avatar {
        height: 2rem;
        width: 2rem;
        border-radius: 50%;
        background: #dfdfdf;
        overflow: hidden;
        img {
          width: 100%;
          height: 100%;
        }
      }

      .info {
        margin-left: 0.52rem;
        width: calc(100% - 3rem);

        .username {
          .name {
            font-size: 0.72rem;
            margin-right: 0.8rem;
            font-weight: bold;
          }

          .vip-status {
            font-size: 0.48rem;
            background: #e6f2ff;
            color: #0078ff;
            display: inline-block;
            height: 0.76rem;
            border-radius: 0.38rem;
            line-height: 0.76rem;
            padding: 0 0.3rem;
          }
        }

        .other-info {
          margin-top: 0.4rem;
          font-size: 0.52rem;

          .expire {
            margin-right: 6%;
          }

          .copy {
            text-decoration: underline;
            margin-left: 3.33%;
            color: #0078ff;
          }
        }
      }
    }
  }
  .equity-img {
    width: 100%;
    img {
      width: 100%;
    }
  }
  .mall-slider {
    background: #fff;
  }
  .vip-item-container {
    background: #fff;
    .title {
      font-size: 0.64rem;
      color: #444;
      height: 2rem;
      line-height: 2rem;
      text-align: center;
      font-weight: bold;
    }
    .vip-item {
      margin: 0 0.48rem;
      img {
        width: 100%;
      }
    }
  }
}
</style>
